package net.ba.map
{
	import com.google.maps.LatLng;
	import com.google.maps.interfaces.IMap;
	import com.google.maps.interfaces.IOverlay;
	import com.google.maps.overlays.Polygon;
	import com.google.maps.overlays.Polyline;
	import com.google.maps.overlays.PolylineOptions;
	import com.google.maps.styles.StrokeStyle;
	
	import flash.events.Event;

	public class FixedOneDegreeGridOverlay implements IOverlay
	{
		
		private var map:IMap;
		private var linesArray:Array;
		private var lineColor:Number;
		private var linesHided:Boolean;
		private var lineOptions:PolylineOptions = new PolylineOptions();
		private var ss:StrokeStyle = new StrokeStyle();		
		
		public function FixedOneDegreeGridOverlay()
		{
		}

		public function initialize(map_:IMap, arg1:Number=NaN):void
		{
			this.map = map_;
			this.linesArray = new Array();
			linesHided=true;
			ss.thickness = 1;
			ss.alpha = 0.7;
			lineOptions.strokeStyle = ss;
			
			this.redraw(true);
			
		}
		
		public function remove():void
		{
			for each (var l:Polyline in this.linesArray) {
				this.map.removeOverlay(l);
			}
			linesArray = new Array();
		}
		
		public function show():void
		{
			linesHided = false;
			lineOptions.strokeStyle.alpha = 0.7;
			for each(var line:Polyline in linesArray) {
				line.setOptions(lineOptions);
			}
		}
		
		public function isHidden():Boolean
		{
			return linesHided;
		}
		
		public function hide():void
		{
			lineOptions.strokeStyle.alpha = 0;
			for each(var line:Polyline in linesArray) {
				line.setOptions(lineOptions);
			}
			linesHided = true;
		}
		
		public function redraw(force:Boolean):void
		{
			//best color for writing on the map
  			lineColor = this.map.getCurrentMapType().getTextColor();
			ss.color = lineColor;
			lineOptions.strokeStyle = ss;
			
			var line:Polyline;
			var pol:Polygon;
			//Vertical
			for(var v:Number = -180;v<=180;v++) {
				line = new Polyline([new LatLng(85,v),new LatLng(-85,v)],lineOptions);
				linesArray.push(line);
				this.map.addOverlay(line);
			}
			
			//Horizontal
			var h:Number;
 			for(h = -85;h<=85;h++) {
				line = new Polyline([new LatLng(h,0.00001),new LatLng(h,179.9999)],lineOptions);
				linesArray.push(line);
				this.map.addOverlay(line);
				
				line = new Polyline([new LatLng(h,-179.9999),new LatLng(h,-0.00001)],lineOptions);
				linesArray.push(line);
				this.map.addOverlay(line);				
			} 
			
		}
		
		public function get interfaceChain():Array
		{
			return null;
		}
		
		public function get wrapper():Object
		{
			return null;
		}
		
		public function set wrapper(arg0:Object):void
		{
		}
		
		public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
		}
		
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
		}
		
		public function dispatchEvent(event:Event):Boolean
		{
			return false;
		}
		
		public function hasEventListener(type:String):Boolean
		{
			return false;
		}
		
		public function willTrigger(type:String):Boolean
		{
			return false;
		}
		
	}
}